package com.subway.babynote;
/*水*/
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;

public class growthinfo extends Activity {
	private static final int INVISIBLE = 0x00000004;
	String[] food_array;
	NotedbHelper notedb;
	ListView height_lv, weight_lv;
	Cursor height_cur, weight_cur;
	TextView heightcomments_view, weightcomments_view,userage_view;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;
	Bundle bundle;
	int age;
	int gender;
	String[] from;
	int[] to;
	Button login_button;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.growthinfo);
		setTitle(R.string.growthinfo);
		notedb = new NotedbHelper(this);
		In = new Intent();
		//initTabFunction();
		height_lv = (ListView) findViewById(R.id.height_list);
		weight_lv = (ListView) findViewById(R.id.weight_list);
		heightcomments_view = (TextView) findViewById(R.id.heightcomments_view);
		weightcomments_view = (TextView) findViewById(R.id.weightcomments_view);
		userage_view = (TextView) findViewById(R.id.userinfo_TextView);
		bundle = this.getIntent().getExtras();
		login_button = (Button) findViewById(R.id.login_button);
		setInfoFromDB();
	
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent In = new Intent();
				In.setClass(growthinfo.this, login.class);
				startActivity(In);
			}
		});
	}

	public void setInfoFromDB() {

		try {

			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			int age = syncapi.getSyncImpl().getUserAge();
			if(age==-1){
				In.setClass(growthinfo.this, birth.class);
				startActivity(In);
			}
			int gender = notedb.getUserGender();
			height_cur = notedb.getHeight(age, gender);
			Log.v("growthinfo", "after get Height");
			from = new String[] { "min", "mid", "max" };
			to = new int[] { R.id.item1, R.id.item2, R.id.item3, };
			ListAdapter listAdapter1 = new SimpleCursorAdapter(this,
					R.layout.hw_item, height_cur, from, to);
			this.startManagingCursor(height_cur);
			height_lv.setAdapter(listAdapter1);
			float height = bundle.getFloat("height");

			String comments = getResources().getString(R.string.height) + height +getResources().getString(R.string.cm) ;
	        int col = height_cur.getColumnIndex("min");
			int col2 = height_cur.getColumnIndex("max");
			float a = height_cur.getFloat(col);
			Log.v("growthinfo", " A=" + a);
		
			if (gender == 0) {
					userage_view.setText(getResources().getString(R.string.babygirl)+ age +getResources().getString(R.string.monthold));
			} else {
					userage_view.setText(getResources().getString(R.string.babyboy) + age + getResources().getString(R.string.monthold));
			}
			if (height < height_cur.getFloat(col)) {
				heightcomments_view.setText(comments + "低于正常值");
			} else if (height > height_cur.getFloat(col2)) {
				heightcomments_view.setText(comments + "高于正常值");
			} else {
				heightcomments_view.setText(comments + "正常");
			}

			weight_cur = notedb.getWeight(age, gender);

			ListAdapter listAdapter2 = new SimpleCursorAdapter(this,
					R.layout.hw_item, weight_cur, from, to);
			weight_lv.setAdapter(listAdapter2);
			this.startManagingCursor(weight_cur);
			float weight = bundle.getFloat("weight");
			comments =getResources().getString(R.string.weight)  + weight + getResources().getString(R.string.kg);
			col = weight_cur.getColumnIndex("min");
			col2 = weight_cur.getColumnIndex("max");
			if (weight < weight_cur.getFloat(col)) {
				weightcomments_view.setText(comments + "低于正常值");
			} else if (weight > weight_cur.getFloat(col2)) {
				weightcomments_view.setText(comments + "高于正常值");
			} else {
				weightcomments_view.setText(comments + "正常");
			}
		} catch (Exception e) {
			Log.v("growthinfo", " e=" + e.getMessage());
		}
	}



	public void onDestroy() {
		notedb.close();
		super.onDestroy();
	}
}